using UnityEngine;


    public static class GameobjectExtend 
    {
        #region Gameobject 

        interface IChildView
        {
            /// <summary>
            /// 自动调用
            /// </summary>
            void Load();
        }
        
        /// <summary>
        /// 设置父物体
        /// </summary>
        /// <param name="trans"></param>
        public static void SetParent(this GameObject go,Transform parent)
        {
            var trans = go.transform;
            trans.SetParent(parent);
        }
        
        /// <summary>
        /// 删除所有子物体
        /// </summary>
        /// <param name="trans"></param>
        private static void ClearAllChild(this GameObject go)
        {
            var trans = go.transform;
            for (int i = trans.childCount-1; i >=0; i--)
            {
                Object.Destroy( trans.GetChild(i).gameObject);
            }
        }
        
        /// <summary>
        /// 删除所有子物体
        /// </summary>
        /// <param name="trans"></param>
        public static void ClearAllChildTemp(this GameObject go)
        {
            var trans = go.transform;
            for (int i = trans.childCount-1; i >=0; i--)
            {
                Object.Destroy( trans.GetChild(i).gameObject);
            }
        }
        
        
        /// <summary>
        /// 隐藏所有子物体
        /// </summary>
        /// <param name="trans"></param>
        public static void HideAllChild(this GameObject go)
        {
            var trans = go.transform;
            for (int i = trans.childCount-1; i >=0; i--)
            {
                trans.GetChild(i).gameObject.SetActive(false);
            }
        }
        /// <summary>
        /// 查找子物体（递归查找）  
        /// </summary> 
        /// <param name="trans">父物体</param>
        /// <param name="goName">子物体的名称</param>
        /// <returns>找到的相应子物体</returns>
        public static Transform FindOnChild(this GameObject gameObject, string goName)
        {
            var trans = gameObject.transform;
            Transform child = trans.Find(goName);
            if (child != null)
                return child;

            Transform go = null;
            for (int i = 0; i < trans.childCount; i++)
            {
                child = trans.GetChild(i);
                go = FindOnChild(child.gameObject, goName);
                if (go != null)
                    return go;
            }

            return null;
        }

        /// <summary>
        /// 查找子物体（递归查找）  where T : UnityEngine.Object
        /// </summary> 
        /// <param name="trans">父物体</param>
        /// <param name="goName">子物体的名称</param>
        /// <returns>找到的相应子物体</returns>
        public static T FindOnChild<T>(this GameObject gameObject, string goName) where T : UnityEngine.Object
        {
            var trans = gameObject.transform;
            Transform child = trans.Find(goName);
            if (child != null)
            {
                return child.GetComponent<T>();
            }

            Transform go = null;
            for (int i = 0; i < trans.childCount; i++)
            {
                child = trans.GetChild(i);
                go = FindOnChild(child.gameObject, goName);
                if (go != null)
                {
                    return go.GetComponent<T>();
                }
            }

            return null;
        }

        public static T GetComponentByPath<T>(this GameObject gameObject, string path) where T : Object
        {
            var trans = gameObject.transform;
            Transform child = trans.Find(path);
            if (child != null)
            {
                return child.GetComponent<T>();
            }

            return null;
        }
        
        static public T AddMissingComponent<T> (this GameObject go) where T : Component
        {
            T t = go.GetComponent<T>();
            if (t == null)
                t = go.AddComponent<T>();
            return t;
        }

        #endregion
}

